package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.BatchQueryConfItemListReq;
import com.sg.dto.base.component.req.DeleteConfItemReq;
import com.sg.dto.base.component.req.QueryConfItemDetailReq;
import com.sg.dto.base.component.req.QueryConfItemListReq;
import com.sg.entity.OmsConfItem;
import com.sg.service.base.component.MOmsConfItemService;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsConfItemMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsConfItemServiceImpl
        extends ServiceImpl<OmsConfItemMapper, OmsConfItem>
        implements MOmsConfItemService {

    @Autowired
    OmsConfItemMapper omsConfItemMapper;

    /**
     * 2-3-02批量查询配置项列表[1997]
     * gen by moon at 10/11/2022, 6:06:39 PM
     */
    @Trace(operationName = "2-3-02批量查询配置项列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsConfItem> batchQueryConfItemList(BatchQueryConfItemListReq reqDto) {

        try {
            QueryWrapper<OmsConfItem> queryWrapper = new QueryWrapper<>();

            if (reqDto.getConfItemList() == null || reqDto.getConfItemList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("conf_item_id", reqDto.getConfItemList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsConfItem.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsConfItem.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsConfItem.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsConfItem>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-02批量修改配置项[2045]
     * gen by moon at 10/11/2022, 6:06:40 PM
     */
    @Trace(operationName = "2-3-02批量修改配置项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateConfItem(List<OmsConfItem> omsConfItem) {

        if (omsConfItem == null || omsConfItem.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsConfItem);

    }

    /**
     * 2-3-02修改配置项[1907]
     * gen by moon at 10/11/2022, 6:06:39 PM
     */
    @Trace(operationName = "2-3-02修改配置项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateConfItem(OmsConfItem omsConfItem) {
        omsConfItem.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsConfItem != null && omsConfItem.getSubjectLifeCycle() != null && omsConfItem.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfItem.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsConfItem != null && omsConfItem.getSubjectLifeCycle() != null && omsConfItem.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsConfItem.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsConfItem.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsConfItem != null && omsConfItem.getSubjectLifeCycle() != null && omsConfItem.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfItem.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsConfItem != null && omsConfItem.getSubjectLifeCycle() != null && omsConfItem.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsConfItem.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsConfItem);
    }

    /**
     * 2-3-02查询配置项列表[1906]
     * gen by moon at 10/11/2022, 6:06:38 PM
     */
    @Trace(operationName = "2-3-02查询配置项列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsConfItem> queryConfItemList(QueryConfItemListReq reqDto) {

        try {
            QueryWrapper<OmsConfItem> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsConfItem.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsConfItem.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsConfItem.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsConfItem>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-02查询配置项详情[1905]
     * gen by moon at 4/13/2023, 9:12:30 PM
     */
    @Trace(operationName = "2-3-02查询配置项详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsConfItem queryConfItemDetail(QueryConfItemDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsConfItem> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsConfItem> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsConfItem retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-02删除配置项[1904]
     * gen by moon at 10/11/2022, 6:06:37 PM
     */
    @Trace(operationName = "2-3-02删除配置项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteConfItem(DeleteConfItemReq reqDto) {
        reqDto.setSubjectLifeCycle("EDITING");
        try {
            Map<String, Object> columnMap = CommonFunctionHelper.objectToMap(reqDto);
            return super.removeByMap(columnMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-02新增配置项[1903]
     * gen by moon at 10/11/2022, 6:06:36 PM
     */
    @Trace(operationName = "2-3-02新增配置项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addConfItem(OmsConfItem omsConfItem) {
        omsConfItem.setConfItemId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsConfItem.setSubjectLifeCycle("EDITING");
        omsConfItem.setIsArchive("FALSE");
        omsConfItem.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsConfItem.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsConfItem != null && omsConfItem.getSubjectLifeCycle() != null && omsConfItem.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfItem.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsConfItem.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsConfItem.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsConfItem != null && omsConfItem.getSubjectLifeCycle() != null && omsConfItem.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfItem.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsConfItem.setIsValid("FALSE");
        omsConfItem.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsConfItem.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsConfItem)) {
            return omsConfItem.getConfItemId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-3-02批量删配置项[3073]
     * gen by moon at 10/11/2022, 6:06:41 PM
     */
    @Trace(operationName = "2-3-02批量删配置项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteConfItem(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }
}
